package com.example.userp.app.myapplication.View;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.userp.app.myapplication.R;

/**
 * Describe:
 * Created by ysy on 2018-08-29.
 */
public class HandWriting extends View {

    private Path mPath; //记录笔画
    private Paint mPaint; //画笔
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private int mLastX;
    private int mLastY;

    private int paintcolor ;
    private float paintstrokewidth;

    private int mPaintColor;
    private float mStrokeWidth;


    public HandWriting(Context context) {
        super(context);
        Init();
    }

    public HandWriting(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.handwriting);
        mPaintColor = typedArray.getColor(R.styleable.handwriting_handwriting_color, context.getResources().getColor(R.color.colorAccent));
        mStrokeWidth = typedArray.getFloat(R.styleable.handwriting_handwriting_width, 10f);
        Init();
    }

    public HandWriting(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.handwriting);
        mPaintColor = typedArray.getColor(R.styleable.handwriting_handwriting_color, context.getResources().getColor(R.color.colorAccent));
        mStrokeWidth = typedArray.getFloat(R.styleable.handwriting_handwriting_width, 10f);
        Init();
    }

    public void Init() {
        mPath = new Path();
        mPaint = new Paint();   //初始化画笔
        mPaint.setColor(mPaintColor);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND); //结合处为圆角
        mPaint.setStrokeCap(Paint.Cap.ROUND); // 设置转弯处为圆角
        mPaint.setStrokeWidth(mStrokeWidth);   // 设置画笔宽度
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        int measuredHeight = getMeasuredHeight();
        mBitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mCanvas.drawPath(mPath,mPaint);
        canvas.drawBitmap(mBitmap,0,0,mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int x= (int) event.getX();
        int y = (int) event.getY();

        switch (action){
            case MotionEvent.ACTION_DOWN:
                mLastX=x;
                mLastY=y;
                mPath.moveTo(mLastX,mLastY);
                break;
            case MotionEvent.ACTION_MOVE:
//                int dx = Math.abs(x - mLastX);
//                int dy = Math.abs(y - mLastY);
//                if (dx > 3 || dy > 3)
                    mPath.lineTo(x, y);
//                mLastX = x;
//                mLastY = y;
                break;
        }
        invalidate();
        return true;
    }
}
